texture ColorRT;
texture LightRT;
texture NormalRT;
texture DepthRT;
texture BlurredScene;
float2 halfPixel;

sampler colorSampler = sampler_state
{
    Texture = (ColorRT);
    AddressU = CLAMP;
    AddressV = CLAMP;
    MagFilter = LINEAR;
    MinFilter = LINEAR;
    Mipfilter = LINEAR;
};

sampler blurredSampler = sampler_state
{
    Texture = (BlurredScene);
    AddressU = CLAMP;
    AddressV = CLAMP;
    MagFilter = LINEAR;
    MinFilter = LINEAR;
    Mipfilter = LINEAR;
};


sampler lightSampler = sampler_state
{
    Texture = (LightRT);
    AddressU = CLAMP;
    AddressV = CLAMP;
    MagFilter = LINEAR;
    MinFilter = LINEAR;
    Mipfilter = LINEAR;
};

sampler normalSampler = sampler_state
{
    Texture = (NormalRT);
    AddressU = CLAMP;
    AddressV = CLAMP;
    MagFilter = LINEAR;
    MinFilter = LINEAR;
    Mipfilter = LINEAR;
};

sampler depthSampler = sampler_state
{
    Texture = (DepthRT);
    AddressU = CLAMP;
    AddressV = CLAMP;
    MagFilter = LINEAR;
    MinFilter = LINEAR;
    Mipfilter = LINEAR;
};

struct VertexShaderInput
{
    float3 Position : POSITION0;
    float2 TexCoord : TEXCOORD0;
};

struct VertexShaderOutput
{
    float4 Position : POSITION0;
    float2 TexCoord : TEXCOORD0;
};

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
    VertexShaderOutput output;
    output.Position = float4(input.Position,1);
    output.TexCoord = input.TexCoord - halfPixel;
    return output;
}

float EdgeDetect(VertexShaderOutput input)
{
	float invWidth = 1.0f/1024.0f;
	float invHeight = 1.0f/600.0f;
	
	float depthCenter = tex2D(depthSampler,input.TexCoord).r;
	
	float depthLeftUp = tex2D(depthSampler,input.TexCoord + float2(-invWidth, -invHeight)).r;
	float depthLeftDown = tex2D(depthSampler,input.TexCoord + float2(-invWidth, invHeight)).r;
	
	float depthRightUp = tex2D(depthSampler,input.TexCoord + float2(invWidth, -invHeight)).r;
	float depthRightDown = tex2D(depthSampler,input.TexCoord + float2(invWidth, invHeight)).r;
	
	float isEdge = ((depthCenter - depthLeftUp) > 0.001f) || 
					((depthCenter - depthLeftDown) > 0.001f) || 
					((depthCenter - depthRightUp) > 0.001f) || 
					((depthCenter - depthRightDown) > 0.001f);
					
	return isEdge;
}

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
	float4 emissive = tex2D(colorSampler, input.TexCoord);
	float4 blurred = tex2D(blurredSampler, input.TexCoord);
	float4 diffuse = tex2D(lightSampler,input.TexCoord);
	float4 finalColor = lerp(diffuse, emissive, emissive.a);
	//clip(length(finalColor.rgb) * 0.99f - 1.0f);
    return finalColor;
}

technique Technique1
{
    pass Pass1
    {
        VertexShader = compile vs_2_0 VertexShaderFunction();
        PixelShader = compile ps_2_0 PixelShaderFunction();
    }
}